!function () {
    var $$ = Dom7;

    $$.ajax({
        url:'http://api.nldou.com/weixinapi/init_wx_jsapi',
        method:"GET",
        data: {fromUrl:window.location.href},
        dataType:'json',
        success: function (res) {
            //console.log('初始化jssdk',res);
            if(res.code==1){
                wx.config({
                    debug: false,
                    appId: res.data.appid,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','hideMenuItems']
                });
            }
        }
    });

    wx.ready(function () {
        wx.hideMenuItems({
            menuList: ['menuItem:share:qq','menuItem:share:weiboApp','menuItem:share:facebook','menuItem:share:QZone',
                'menuItem:copyUrl','menuItem:openWithQQBrowser','menuItem:openWithSafari']
        });
        var query = window.location.search;
        var audioqid = query.substr((query.indexOf('=')+1));
        var shareobj = {url:'',title:''};
        shareobj.url = 'http://api.nldou.com/ucenter/weixinlogin?pid=7653&page=question&param=qid.'+audioqid;
        var audiosrc = '';

        function sharecallback(sharetype)
        {
            $$.ajax({
                method: 'POST',
                url: 'http://api.nldou.com/commwapucenter/sharecallback',
                data: {uid:0,url:shareobj.url,type:sharetype},
                dataType: 'json',
                success: function (res) {return true;}
            });
        }

        $$.ajax({
            method:'POST',
            url:'http://api.nldou.com/commwapaudio/getAudio',
            data:{qid:audioqid},
            dataType:'json',
            async:false,
            success:function (res) {
                //console.log('音频页面',res);
                if (res.code==1){
                    audiosrc = res.data.audio;
                    shareobj.title = res.data.title;
                    $$('#Audio .audiotitle').html(shareobj.title);
                }
            }
        });

        var barlength = Math.ceil($$('.slidebar').width());//拖拽轴的长度
        var startpos = 0;//拖拽开始的位置，相对于拖拽轴的左端，单位px
        var endpos = 0;//拖拽结束的位置，相对于拖拽轴的左端，单位px
        var startpagex;//拖拽开始的位置，相对于屏幕的左端，单位px
        var audioplaying = false;//音频的播放状态
        var audioid;
        var audiocounter;//计时器
        var curtime = 0;
        var duration,tpx,pxt,min,sec;

        var audioanimid;
        function audioanim(){
            var r = parseFloat($$("#Audio .audiocover").data('rotate'));
            r = r>360?(r-360+0.2):(r+0.2);
            $$("#Audio .audiocover").transform('rotate('+r+'deg)');
            $$("#Audio .audiocover").data('rotate',r);
            audioanimid = $$.requestAnimationFrame(audioanim);
        }

        var sound = new Howl({
            src:[audiosrc],
            html5:true
        });
        sound.once('load',function () {
            $$("#Audio .slide-btn span").hide();
            duration = sound.duration();
            tpx = duration/barlength;//(s/px)
            pxt = barlength/duration;//(px/s)
            //格式化总时间
            min = Math.floor(duration/60).toString();
            min = min.length>1?min:'0'+min;
            sec = Math.round(duration%60).toString();
            sec = sec.length>1?sec:'0'+sec;
            $$('.totaltime').html(min+':'+sec);
            //播放/暂停
            $$(".play-btn").on('click',function () {
                if (!audioplaying){
                    audioid = sound.play();
                }else{
                    sound.pause(audioid);
                }
            });
            //拖拽时间轴
            $$(".slide-btn").on('touchstart',function(e){
                if (audioid){
                    sound.pause(audioid);
                    startpos = endpos;
                    startpagex = e.touches[0].pageX;
                }
            });
            $$(".slide-btn").on('touchmove',function(e){
                if (audioid) {
                    var p = e.touches[0].pageX - startpagex + startpos;
                    if (p > -1 && p < barlength) {
                        endpos = p;
                        $$(this).parent().css('width', endpos + 'px');

                        curtime = endpos * tpx;
                        var min = Math.floor(curtime / 60).toString();
                        min = min.length>1?min:'0'+min;
                        var sec = Math.round(curtime % 60).toString();
                        sec = sec.length > 1 ? sec : '0' + sec;
                        $$('.curtime').html(min + ':' + sec);
                    }
                }
            });
            $$(".slide-btn").on('touchend',function () {
                if (audioid) {
                    sound.seek(curtime,audioid);
                }
            });
            sound.on('play',function () {
                audioplaying = true;
                $$(".play-btn").find('i').html('pause_round');
                audiocounter = setInterval(function () {
                    curtime++;
                    //格式化时间
                    min = Math.floor(curtime/60).toString();
                    min = min.length>1?min:'0'+min;
                    sec = Math.round(curtime%60).toString();
                    sec = sec.length>1?sec:'0'+sec;
                    $$('.curtime').html(min+':'+sec);
                    endpos = pxt*curtime;
                    $$('.probar').css('width', endpos + 'px');
                },1000);
                audioanimid = $$.requestAnimationFrame(audioanim);
            });
            sound.on('seek',function (id) {
                sound.play(id);
            });
            sound.on('pause',function () {
                audioplaying=false;
                clearInterval(audiocounter);
                $$(".play-btn").find('i').html('play_round');
                $$.cancelAnimationFrame(audioanimid);
            });
            sound.on('end',function () {
                audioplaying=false;
                clearInterval(audiocounter);
                $$(".play-btn").find('i').html('play_round');
                curtime = 0;
                $$('.curtime').html('00:00');
                endpos = 0;
                $$('.probar').css('width', endpos + 'px');
                $$.cancelAnimationFrame(audioanimid);
            })
        });

        wx.onMenuShareTimeline({
            title: shareobj.title,
            link: shareobj.url,
            imgUrl: 'http://wap.nldou.com/comm/img/comm-logo.jpg',
            success: function () {
                sharecallback('timeline');
                return true;
            }
        });
        wx.onMenuShareAppMessage({
            title: shareobj.title,
            desc: '复旦大学社会学教授沈奕斐，为您解答家庭教育的各类问题，陪您共走育儿路',
            link: shareobj.url,
            imgUrl: 'http://wap.nldou.com/comm/img/comm-logo.jpg',
            type: 'link',
            success: function () {
                sharecallback('appmsg');
                return true;
            }
        });
    });
}();
